const app = getApp();
Page({
  data:{
    Math_q: app.globalData.Math_question,
    Math_a: app.globalData.Choose,
    Correct: app.globalData.Correct_Answer,
    question: "",
    display:[],
    Choose:['A','B','C','D','E','F'],
    index:0,
    qhaveImage:null,
    chaveImage:[]
  },

shuffle:function(){
  return Math.random()>0.5?1:-1
}
,
//打乱choose顺序
modify:function(){
  this.setData({
    Choose: this.data.Choose.sort(this.shuffle)
  })
},

//下一道题
changeData:function(){
  this.data.index = this.data.index + 1
  this.setData({
    index:this.data.index
  })
  this.Start()
},


//按钮监听，将错题添加至全局变量中
judge:function(e){
  if(this.data.index < this.data.Math_q[0].length - 1){
  if(e.currentTarget.dataset.name != this.data.Correct[0][this.data.index]){
    app.globalData.WrongQuestion.push(this.data.Math_q[0][this.data.index])
    app.globalData.WrongAnswer.push(this.data.display)
    app.globalData.WrongChoose.push(e.currentTarget.dataset.name)
    app.globalData.Correct.push(this.data.Correct[0][this.data.index])
  }
  this.changeData()
  }
  else{
    this.jump()
  }
},

//跳转结果页
jump:function(){
  wx.redirectTo({
    url: '/pages/Math_result/Math_result?Number='+app.globalData.WrongQuestion.length+'&Sum='+this.data.Math_q[0].length,
  })
},

//初始化数据
Start:function(){
  this.data.display = []
  this.data.chaveImage = []
  this.data.question = this.data.Math_q[0][this.data.index]['content']
  var rowChoose = this.data.Math_a[0][this.data.index]
  for(let i=0;i<this.data.Choose.length;i++){
    if(rowChoose[this.data.Choose[i]]!= ''){
      this.data.display.push(rowChoose[this.data.Choose[i]])
      this.data.chaveImage.push(rowChoose[this.data.Choose[i]].indexOf('http')>=0)
    }
  }

  this.data.qhaveImage = (this.data.Math_q[0][this.data.index]['q_src1'] != '' || this.data.Math_q[0][this.data.index]['q_src2'] != '' ) 
  this.setData({
    display: this.data.display,
    question: this.data.question,
    qhaveImage: this.data.qhaveImage,
    chaveImage: this.data.chaveImage
  })
},

//界面显示
  onShow:function(){
      this.modify()
      this.Start()
  },

})
